<template>
  <div class="article-comment">
    <!-- 全部评论 -->
    <van-sticky offset-top="11.73333vw">
      <div class="title van-hairline--bottom">
        <span>全部评论 ({{ info.comm_count }})</span>
        <span>0 点赞</span>
      </div>
    </van-sticky>
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="到底了"
      @load="getData"
    >
      <div class="list">
        <div
          class="item van-hairline--bottom"
          v-for="i in commentList"
          :key="i.com_id"
        >
          <van-image round width="10vw" height="10vw" :src="i.aut_photo" />
          <div class="info">
            <p>
              <span class="name">{{ i.aut_name }}</span>
              <span class="zan"
                >{{ i.like_count }} <geek-icon name="like2"
              /></span>
            </p>
            <p class="cont">{{ i.content }}</p>
            <p>
              <span class="reply" @click="reply(i)"
                >回复 <i class="van-icon van-icon-arrow"></i
              ></span>
              <span class="time">{{ i.pubdate | filterTimeGlobal }}</span>
            </p>
          </div>
        </div>
      </div>
    </van-list>
    <!-- 底部工具 -->
    <div class="footer van-hairline--top">
      <div class="input" @click="sendComment">
        <i class="van-icon van-icon-edit"></i>
      </div>
      <div class="btn" @click="$emit('scrollToSet')">
        <geek-icon name="comment"></geek-icon>
        <p>评论</p>
        <i>{{ info.comm_count }}</i>
      </div>
      <div class="btn" @click="likeClick">
        <geek-icon
          :name="info.attitude === -1 ? 'like-sel' : 'like'"
        ></geek-icon>
        <p>点赞</p>
      </div>
      <div class="btn" @click="clickArticle">
        <geek-icon
          :name="info.is_collected ? 'collect-sel' : 'collect'"
        ></geek-icon>
        <p>收藏</p>
      </div>
      <div class="btn">
        <geek-icon name="share"></geek-icon>
        <p>分享</p>
      </div>
    </div>
    <ReComment
      v-model="Show"
      :loaclItem="loaclItem"
      :commentList="commentList"
      :info="info"
     ref="reComment"
    ></ReComment>
  </div>
</template>
<script>
import {
  commentsGet,
  articleLikings,
  articleCollections,
  commentsPost
} from '@/api/article/index.js'
import ReComment from './re-comment.vue'
export default {
  name: 'ArticleComment',
  components: {
    ReComment
  },
  props: ['info'],
  data () {
    return {
      params: {
        type: 'a', //评论类型
        source: this.$route.params.id, // 文章id
        offset: null, //获取评论数据的偏移量
        limit: 1 //默认每页评论数量
      },
      commentList: [],
      total: 0,
      loading: false,
      finished: false,
      Show: false,
      loaclItem: '',
      showSend: false,
      
      rules: [
        {
          required: true,
          message: '必填',
          trigger: 'onChange'
        }
      ],
      bol: true
    }
  },

  created () {
    // this.getData()
  },
  methods: {
    async getData () {
      const res = await commentsGet(this.params)
      this.loading = false //修改loading胃FALSE
      if (res.data.results.length > 0) {
        this.commentList.push(...res.data.results)
      }
      this.total = res.data.total_count
      //判断数据是否加载完成
      if (res.data.end_id === res.data.last_id) {
        this.finished = true
      } else {
        this.params.offset = res.data.last_id
      }
      console.log('评论', res)
    },
    //点赞
    async likeClick () {
      await articleLikings(this.$route.params.id, this.info.attitude === -1)
      this.info.attitude = this.info.attitude * -1
      this.$toast.success(this.info.attitude === -1 ? '点赞成功' : '取消点赞')
    },
    async clickArticle () {
      await articleCollections(this.$route.params.id, this.info.is_collected)
      this.info.is_collected = !this.info.is_collected
      this.$toast.success(this.info.is_collected ? '收藏成功' : '取消收藏')
    },
    //回复评论
    reply (item) {
      this.Show = true
      //props
      this.loaclItem = item
    },
    //发表评论
    sendComment () {
      //大开弹框
       this.$refs.reComment.showSend = true
      this.loaclItem = ''
      this.$emit('scrollToSet')
    }
  }
}
</script>
<style scoped lang="less">
.van-popup {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.article-comment {
  .title {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px;
    background: #fff;
    span {
      font-size: 16px;
      &:last-child {
        color: #ccc;
        font-size: 14px;
      }
    }
  }
  .list {
    padding: 0 16px 30px;
    .item {
      display: flex;
      padding: 10px 0;
      .info {
        padding-left: 10px;
        flex: 1;
        p {
          margin: 0;
          .name {
            font-size: 16px;
          }
          .zan {
            font-size: 14px;
            float: right;
            color: #999;
            .geek-icon {
              font-size: 12px;
              position: relative;
              top: -1px;
            }
          }
          &.cont {
            font-size: 14px;
            color: #666;
            padding: 10px 0;
            word-break: break-all;
            padding-right: 40px;
          }
          .reply {
            min-width: 60px;
            height: 24px;
            text-align: center;
            line-height: 28px;
            font-size: 12px;
            background: @geek-gray-color;
            display: inline-block;
            border-radius: 14px;
            color: #666;
            .van-icon {
              position: relative;
              top: 1px;
            }
          }
          .time {
            font-size: 12px;
            color: #999;
            margin-left: 10px;
          }
        }
      }
    }
  }
}

.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 50px;
  background: #fff;
  display: flex;
  width: 100%;
  align-items: center;
  .input {
    margin-left: 10px;
    width: 200px;
    height: 34px;
    background: @geek-gray-color;
    border-radius: 17px;
    line-height: 36px;
    padding-left: 10px;
    .van-icon {
      color: #999;
    }
  }
  .btn {
    flex: 1;
    text-align: center;
    position: relative;
    p {
      margin: 0;
      font-size: 10px;
    }
    .geek-icon {
      font-size: 18px;
    }
    i {
      height: 16px;
      min-width: 16px;
      padding: 0 3px;
      background: @geek-color;
      color: #fff;
      font-size: 10px;
      position: absolute;
      right: 0;
      top: -4px;
      line-height: 16px;
      border-radius: 8px;
      font-style: normal;
    }
  }
}
</style>
